﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace AI.Graph {


    [Serializable]
    public class Parameter : ScriptableObject {

        [SerializeField]
        private string m_typeName;

        [SerializeField]
        private List<Node> m_boundNodes = new List<Node> ();

        public Type type {
            get {
                if (string.IsNullOrEmpty (m_typeName)) {
                    return null;
                }

                return Type.GetType (m_typeName);
            }
            set {
                if (value == null) {
                    m_typeName = null;
                }
                else {
                    m_typeName = value.AssemblyQualifiedName;
                }
            }
        }


        public Node[] boundNodes {
            get { return m_boundNodes.ToArray (); }
        }

        public bool hasBoundedNodes {
            get { return m_boundNodes.Count > 0; }
        }


        void OnEnable () {
            hideFlags = HideFlags.HideInHierarchy;
        }


        /// <summary>
        /// Binds the given type to the given node.
        /// </summary>
        public void Bind (Node node, Type type) {
            if (!ContainsBoundedNode (node)) {
                m_boundNodes.Add (node);
                this.type = type;
            }
        }


        /// <summary>
        /// Resets the type of the given node.
        /// </summary>
        public void Unbind (Node node) {
            if (ContainsBoundedNode (node)) {
                m_boundNodes.Remove (node);
                if (m_boundNodes.Count == 0) {
                    type = null;
                }
            }
        }


        public bool ContainsBoundedNode (Node node) {
            return m_boundNodes.Contains (node);
        }


        public void UnbindAll () {
            m_boundNodes.Clear ();
            type = null;
        }

    }


}